package com.app.system.utils.dataType;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by ZYK on 2019/1/16.
 */
public class IntegerUtils {
    /**
     * 空值处理
     *
     * @param integer
     * @return
     */
    public static Integer getNotNull(Integer integer) {
        return integer == null ? 0 : integer;
    }

    public static String getNotNullStr(Integer value) {
        return value == null || value == 0 ? "" : value + "";
    }

    public static Integer getIntAndIntSub(Integer integer1,Integer integer2){
        return getNotNull(integer1) - getNotNull(integer2);
    }

    /**
     * 计算求和
     *
     * @param values
     * @return
     */
    public static Integer getSum(Integer... values) {
        Integer result = 0;
        if (values != null && values.length > 0) {
            for (Integer v : values) {
                result += getNotNull(v);
            }
        }
        return result;
    }

    /**
     * string转Integer类型
     * @param value
     * @return
     */
    public static Integer stringToInteger(String value) {
        if (StringUtils.isNotEmpty(value)) {
            try {
                return new Integer(value);
            } catch (Exception e) {
                return 0;
            }
        } else {
            return 0;
        }
    }
    /**
     * 判断是否还有下一页
     *
     * @param pageNum
     * @param num1
     * @param num2
     * @return
     */
    public static boolean compare(Integer pageNum, Integer num1, Integer num2) {
        Boolean hasNext = true;
        pageNum = getNotNull(pageNum);
        num1 = getNotNull(num1);
        num2 = getNotNull(num2);
        hasNext = pageNum < getDiv(num1,num2) ? true : false;
        return hasNext;
    }
    /**
     * 相除
     * @param num1
     * @param num2
     * @return
     */
    public static Integer getDiv(Integer num1,Integer num2){
        Integer result = 0;
        int remainder = 0;
        if (num1 != 0 && num2 != 0) {
            remainder = num1 % num2;
        }
        num1 = getNotNull(num1);
        num2 = getNotNull(num2);
        if (num1 != 0 && num2 != 0) {
            result = num1 / num2;
        }
        if(remainder > 0 && result > 0){
            result = result +1;
        }
        return result;
    }

    /**
     * object数值转化为int
     * @param object
     * @return
     */
    public static Integer objectToInteger(Object object) {
        if(object == null || object.equals("")){
            return 0;
        }
        return Integer.valueOf(String.valueOf(object));
    }

}
